//
//  KLMUseOccasionManager.swift
//  KLM
//
//  Created by 朱雨 on 2023/12/15.
//

import Foundation

class KLMUseOccasionManager: NSObject {
    
    private var firstList: [KLMType] = [KLMType]()
    private var secondList: [KLMType] = [KLMType]()
    private var allList: [KLMType] = [KLMType]()
    private var firstPopView: YBPopupMenu!
    typealias SelectTypeBlock = (_ type: KLMType) -> Void
    private var selectTypeBlock: SelectTypeBlock?
    
    /// isFresh 是否是生鲜灯
    func showDropView(point: CGPoint, isFresh: Bool? = false, maxVisibleCount: Int? = nil, selectType: @escaping SelectTypeBlock) {
        selectTypeBlock = selectType
        var titles: [String] = [String]()
        if isFresh == false { ///不是生鲜
            for model in firstList {
                titles.append(KLMLocalizable.LANGLOC(model.title))
            }
        } else {
            for model in secondList {
                titles.append(KLMLocalizable.LANGLOC(model.title))
            }
        }
        
        YBPopupMenu.show(at: point, titles: titles, icons: nil, menuWidth: 150) { popupMenu in
            popupMenu?.priorityDirection = .none
            popupMenu?.arrowHeight = 0
            popupMenu?.minSpace = point.x
            popupMenu?.isShadowShowing = false
            popupMenu?.delegate = self
            popupMenu?.cornerRadius = 0
            if let maxVisibleCount = maxVisibleCount {
                popupMenu?.maxVisibleCount = maxVisibleCount
            }
            if isFresh == false {
                popupMenu?.dismissOnSelected = false
                popupMenu?.tag = 100
                self.firstPopView = popupMenu
            }
        }
    }
    
    func getType(num: Int) -> KLMType? {
        let type = allList.first(where: {$0.num == num})
        return type
    }
    
    func getAllList(deviceType: nodeDeviceType? = nil) {
        let str: String = Bundle.main.path(forResource: "OccasionPlist", ofType: "plist")!
        let occations: NSArray = try! NSArray.init(contentsOf: URL.init(fileURLWithPath: str), error: ())
        firstList = KLMTool.jsonToModel(type: KLMType.self, array: occations as! [[String : Any]])!
//        if apptype == .targetGN || apptype == .test { ///移除生鲜 - 果蔬副食
//            ///斗胆灯有果蔬副食
//            if deviceType == .lukaOneCamera || deviceType == .lukaUniOneCamera {
<<<<<<< HEAD
//
=======
//                
>>>>>>> c9ff26f7439158038ece58549346ff469099cbd4
//            } else {
//                firstList.removeAll(where: {$0.num == 1})
//            }
//        }
        
        let str1: String = Bundle.main.path(forResource: "GroceriesPlist", ofType: "plist")!
        let groceries: NSArray = try! NSArray.init(contentsOf: URL.init(fileURLWithPath: str1), error: ())
        secondList = KLMTool.jsonToModel(type: KLMType.self, array: groceries as! [[String : Any]])!
        allList = firstList + secondList
    }
    
    //单例
    static let shared = KLMUseOccasionManager()
    private override init(){
//        super.init()
//        getAllList()
//        let str: String = Bundle.main.path(forResource: "OccasionPlist", ofType: "plist")!
//        let occations: NSArray = try! NSArray.init(contentsOf: URL.init(fileURLWithPath: str), error: ())
//        firstList = KLMTool.jsonToModel(type: KLMType.self, array: occations as! [[String : Any]])!
//        if apptype == .targetGN || apptype == .test { ///移除生鲜 - 果蔬副食
//            firstList.removeAll(where: {$0.num == 1})
//        }
<<<<<<< HEAD
//
=======
//        
>>>>>>> c9ff26f7439158038ece58549346ff469099cbd4
//        let str1: String = Bundle.main.path(forResource: "GroceriesPlist", ofType: "plist")!
//        let groceries: NSArray = try! NSArray.init(contentsOf: URL.init(fileURLWithPath: str1), error: ())
//        secondList = KLMTool.jsonToModel(type: KLMType.self, array: groceries as! [[String : Any]])!
//        allList = firstList + secondList
    }
}

extension KLMUseOccasionManager: YBPopupMenuDelegate {
    
    func ybPopupMenu(_ ybPopupMenu: YBPopupMenu!, didSelectedAt index: Int) {
        
        if ybPopupMenu.tag == 100 {
            if let model = firstList.indexValue(safe: index), model.num == 1 { ///果蔬副食，有二级菜单
                let point: CGPoint = CGPoint.init(x: firstPopView.origin.x, y: firstPopView.origin.y)
                var titles: [String] = [String]()
                for model in secondList {
                    titles.append(KLMLocalizable.LANGLOC(model.title))
                }
                YBPopupMenu.show(at: point, titles: titles, icons: nil, menuWidth: 120) { popupMenu in
                    popupMenu?.priorityDirection = .none
                    popupMenu?.maxVisibleCount = 4
                    popupMenu?.arrowHeight = 0
                    popupMenu?.minSpace = self.firstPopView.origin.x + self.firstPopView.width
                    popupMenu?.isShadowShowing = false
                    popupMenu?.delegate = self
                    popupMenu?.cornerRadius = 0
                    popupMenu?.tag = 10
                }
                return
            }
    
            
            ybPopupMenu.dismiss()
            let selectCategory = firstList[index]
            if selectCategory.num == 10 || selectCategory.num == 11 {
<<<<<<< HEAD
//                KLMKeyWindow!.makeToast(KLMLocalizable.LANGLOC(selectCategory.title) + " " + KLMLocalizable.LANGLOC("is a reflective material, and it is normal to reduce the brightness during demo."), duration: 5, position: "center")
=======
                KLMKeyWindow!.makeToast(KLMLocalizable.LANGLOC(selectCategory.title) + " " + KLMLocalizable.LANGLOC("is a reflective material, and it is normal to reduce the brightness during demo."), duration: 5, position: "center")
>>>>>>> c9ff26f7439158038ece58549346ff469099cbd4
            }
            if let selectTypeBlock = selectTypeBlock {
                selectTypeBlock(selectCategory)
            }
        } else { //果蔬二级菜单
            
            firstPopView?.dismiss()
            let selectCategory = secondList[index]
            if let selectTypeBlock = selectTypeBlock {
                selectTypeBlock(selectCategory)
            }
        }
    }
}
